package com.foreverwin.jzymes.report.labor.service.impl;

import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.labor.domain.LaborAttendanceLog;
import com.foreverwin.jzymes.production.labor.mapper.laborSelectLogMapper;
import com.foreverwin.jzymes.production.labor.service.ILaborAttendSerchService;
import com.foreverwin.jzymes.production.labor.service.ILaborAttendanceLogService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.enums.ProcedureEnum;
import com.foreverwin.jzymes.production.sfc.mapper.SFCAdviceMapper;
import com.foreverwin.jzymes.report.labor.mapper.laborReportMapper;
import com.foreverwin.jzymes.report.labor.service.ILaborAttendReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LaborAttendReportServiceImpl implements ILaborAttendReportService {
    @Autowired
    ILaborAttendanceLogService logService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;
    @Autowired
    IFieldGroupAdviceService fieldGroupAdviceService;
    @Autowired
    ISystemRuleAdviceService systemRuleAdviceService;
    @Autowired
    SFCAdviceMapper sfcAdviceMapper;
    @Autowired
    laborReportMapper laborReportMapper;
    @Autowired
    IProductionShiftService productionShiftService;

    /**
     * 人力出勤报表
     *
     * @param jsonMap
     * @return
     */
    public List<LaborAttendanceLog> findLaborAttanceReport(Map<String,Object> jsonMap ){
      String site = (String)jsonMap.get("site");
      String dept = (String)jsonMap.get("deptBo");
      String procedureBo = (String)jsonMap.get("procedure");
      /**校验站点*/
      siteAdviceService.existSite( site );

      Date startDate = null;
      Date endDate = null;
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      if ( !StringUtils.isBlank( (String)jsonMap.get("startDate") ) ){
          try {
              startDate = simpleDateFormat.parse((String)jsonMap.get("startDate")) ;
          } catch (ParseException e) {
              e.printStackTrace();
          }
      }

      if ( !StringUtils.isBlank( (String)jsonMap.get("endDate") ) ){
          try {
              endDate = simpleDateFormat.parse((String)jsonMap.get("endDate")) ;
          } catch (ParseException e) {
              e.printStackTrace();
          }
      }

      if ( !StringUtils.isBlank( procedureBo )){
          procedureBo = BOHandleUtil.buildFieldGroupHandle( site , procedureBo );
      }


        List<LaborAttendanceLog> laborAttendanceLogs =  laborReportMapper.selectLaborAttanceReport( site,startDate,endDate,procedureBo,dept );


        return laborAttendanceLogs;
    }

}
